package com.ruoyi.flowable.listener.move.moveRepair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquMove;
import com.ruoyi.equ.domain.EquRepair;
import com.ruoyi.equ.service.IEquRepairService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 移动-维修-1监听
 */
@Slf4j
@Component("moveRepairListener_1")
public class MoveRepairListener_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //故障信息
            String faultTime = execution.getVariable("faultTime", String.class);
            String faultType = execution.getVariable("faultType", String.class);
            String errorCode = execution.getVariable("errorCode", String.class);
            String faultImpact = execution.getVariable("faultImpact", String.class);
            String urgency = execution.getVariable("urgency", String.class);
            String remark = execution.getVariable("remark", String.class);
            //赋值
            HashMap<String, Object> faultInfoMap = new HashMap<>();
            faultInfoMap.put("故障时间", faultTime);
            faultInfoMap.put("故障现象", faultType);
            if (errorCode.equals("")) {
                faultInfoMap.put("错误代码", "无");
            }
            else {
                faultInfoMap.put("错误代码", errorCode);
            }
            faultInfoMap.put("影响范围", faultImpact);
            faultInfoMap.put("紧急程度", urgency);
            faultInfoMap.put("备注", remark);

            //故障信息添加到->移动-执行信息
            EquMove equMove = execution.getVariable("equMove", EquMove.class);
            Map<String, Object> executeInfoMap = objectMapper.readValue(equMove.getExecuteInfo(), new TypeReference<Map<String, Object>>() {});
            executeInfoMap.put("维修故障信息", faultInfoMap);
            String executeInfo = objectMapper.writeValueAsString(executeInfoMap);

            equMove.setExecuteInfo(executeInfo);
            execution.setVariable("equMove", equMove);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
